package org.lvws.hellospringcloud.helloconsul.consulconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping("/consul")
@RestController
public class ConsulConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/services")
    public String services() {
        return loadBalancerClient.choose("consul-provider").getUri().toString();
    }

    @GetMapping("/discover")
    public Object discover() {
        return discoveryClient.getServices();
    }

    @GetMapping("/hello")
    public String hello() {
//        ServiceInstance serviceInstance = loadBalancerClient.choose("consul-provider");
//        String url = serviceInstance.getUri().toString();
        String url = "http://localhost:8080";
        String result = new RestTemplate().getForObject(url + "consul/hello/123", String.class);
        return result;
    }

}
